/*
 * @Description: 新建单位弹窗
 * @Author: Rfan
 * @Date: 2022-01-07 14:54:11
 * @LastEditTime: 2022-05-26 17:19:30
 */

import { FC, useEffect, useState } from 'react';
import { Button, Form, Modal } from 'antd';
import AddUnitForm from './common/AddUnitForm';
import type { ICommonDialog } from '@/interfaces/global';
import type { IAddSubData } from '../Operate';

interface AddUnitDialogProps extends ICommonDialog {
  data?: IAddSubData;
}

const AddUnitDialog: FC<AddUnitDialogProps> = (props) => {
  console.log('add unit dialog', props.visible);
  const { visible, data, onClose } = props;
  const [form] = Form.useForm();

  // 是否子单位
  const [subFlag, setSubFlag] = useState(false);
  // 新建成功后立即创建全宗 复选框默认不勾选
  const [addFondOnSuccess, setAddFondOnSuccess] = useState(false);

  useEffect(() => {
    if (visible) {
      if (data?.pid) {
        setSubFlag(true);
      } else {
        setSubFlag(false);
      }
    }
  }, [visible]);

  // 新建成功后立即创建全宗 选中状态变化方法
  const handleAddFondChange = (evt: any) => {
    setAddFondOnSuccess(evt.target.checked);
  };

  // 提交表单
  const submitFunc = () => {
    form.submit();
  };

  const onCancel = () => {
    onClose();
  };

  return (
    <Modal
      destroyOnClose
      title={subFlag ? '新建子单位' : '新建单位'}
      visible={visible}
      onCancel={onCancel}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          保存
        </Button>
      ]}
    >
      <AddUnitForm
        form={form}
        data={data}
        jumpToFond={addFondOnSuccess}
        visible={visible}
        closeDialog={() => {
          onCancel();
        }}
      />
    </Modal>
  );
};

export default AddUnitDialog;
